`
https://leetcode.cn/problems/flatten-nested-list-iterator/
`

/**
 * // This is the interface that allows for creating nested lists.
 * // You should not implement it, or speculate about its implementation
 * function NestedInteger() {
 *
 *     Return true if this NestedInteger holds a single integer, rather than a nested list.
 *     @return {boolean}
 *     this.isInteger = function() {
 *         ...
 *     };
 *
 *     Return the single integer that this NestedInteger holds, if it holds a single integer
 *     Return null if this NestedInteger holds a nested list
 *     @return {integer}
 *     this.getInteger = function() {
 *         ...
 *     };
 *
 *     Return the nested list that this NestedInteger holds, if it holds a nested list
 *     Return null if this NestedInteger holds a single integer
 *     @return {NestedInteger[]}
 *     this.getList = function() {
 *         ...
 *     };
 * };
 */
/**
 * @constructor
 * @param {NestedInteger[]} nestedList
 */
var NestedIterator = function(nestedList) {
  // 由于在数组头部插入和删除元素的时间复杂度为 O(n)
  // 所以我们可以先将 nestedList 反转，然后使用 pop() 操作
  // 从后往前取元素，这样就能保证每次操作的时间复杂度为 O(1)
  this.list = nestedList.reverse();
};


/**
* @this NestedIterator
* @returns {boolean}
*/
NestedIterator.prototype.hasNext = function() {
  // 如果列表里有元素，且第一个元素不是数字
  while (this.list.length && !this.list[this.list.length - 1].isInteger()) {
      // 就把第一个元素拿出来，拆开
      const first = this.list.pop().getList();
      // 把拆开的结果按照原顺序依次放回列表里
      for (let i = first.length - 1; i >= 0; i--) {
          this.list.push(first[i]);
      }
  }
  return this.list.length > 0;
};

/**
* @this NestedIterator
* @returns {integer}
*/
NestedIterator.prototype.next = function() {
  // hasNext 方法保证了第一个元素一定是整数类型
  return this.list.pop().getInteger();
};

/**
* Your NestedIterator will be called like this:
* var i = new NestedIterator(nestedList), a = [];
* while (i.hasNext()) a.push(i.next());
*/